﻿Imports System.ServiceModel

Public Class frmCancelaMultiple
    Private _random As New Random

    Private Sub btnCancelaMultiple_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelaMultiple.Click
        Dim transactionId As Long = _random.[Next]()
        Dim transactionIdOriginal As String = String.Empty
        Dim token As String = String.Empty
        Dim Errores As String = String.Empty
        Dim listaUUID As String = txtListaUUID.Text
        Dim resultado As String = String.Empty
        Using Cancelacion = New SrvCancelation.CancelacionesClient()
            Dim Util As Utilerias = New Utilerias()
            Errores = Util.CreateToken(txtRFC.Text, transactionId, token)
            If (String.IsNullOrEmpty(Errores) = False) Then
                ShowUnknownError(Errores)
                Exit Sub
            End If
            Try
                Dim listado() As String = listaUUID.Split(",")
                Dim lista = New SrvCancelation.ListaCancelar()
             
                For Each uuid As String In listado
                    lista.Add(Guid.Parse(uuid))
                Next

                Dim respuesta = Cancelacion.CancelaMultiple(lista, txtRFC.Text, token, transactionId, transactionIdOriginal)
                For Each item In respuesta
                    resultado += String.Format("UUID : {0} Estatus : {1}", item.UUID, item.Estatus)
                Next
                txtRespuesta.Text = resultado
            Catch validatioFault As FaultException(Of SrvCancelation.FallaValidacion)
                Using errorForm = New frmError(TranslateFault.ToValidationError(validatioFault.Detail))
                    errorForm.ShowDialog()
                End Using
            Catch serviceFault As FaultException(Of SrvCancelation.FallaServicio)
                Using errorForm = New frmError(TranslateFault.ToServiceError(serviceFault.Detail))
                    errorForm.ShowDialog()
                End Using
            Catch sessionFauld As FaultException(Of SrvCancelation.FallaSesion)
                Using errorForm = New frmError(TranslateFault.ToSessionError(sessionFauld.Detail))
                    errorForm.ShowDialog()
                End Using
            Catch ex As Exception
                ShowUnknownError(ex.Message)
            End Try
        End Using
    End Sub

    Private Sub ShowSessionError(ByVal sessionError As Entity.SessionError)
        MessageBox.Show(String.Format("Status {0}: {1}", sessionError.Status.ToString(), sessionError.Description), "Ecodex UniTest")
    End Sub

    Private Sub ShowUnknownError(ByVal p1 As String)
        MessageBox.Show(String.Format("Error: {0}", p1), "Ecodex UniTest")
    End Sub

    Private Sub ShowResult(ByVal p1 As String)
        MessageBox.Show(p1, "Ecodex UniTest")
    End Sub
End Class